
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.Company;
import com.cmc.dao.CompanyDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class CompanyDaoHibernate extends BaseDaoHibernate implements CompanyDao {

    /**
     * @see com.cmc.dao.CompanyDao#getCompanys(com.cmc.model.Company)
     */
    public List getCompanys(final Company company) {
        return getHibernateTemplate().find("from Company");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (company == null) {
            return getHibernateTemplate().find("from Company");
        } else {
            // filter on properties set in the company
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(company).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Company.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.cmc.dao.CompanyDao#getCompany(Long id)
     */
    public Company getCompany(final Long id) {
        Company company = (Company) getHibernateTemplate().get(Company.class, id);
        if (company == null) {
            log.warn("uh oh, company with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(Company.class, id);
        }

        return company;
    }

    /**
     * @see com.cmc.dao.CompanyDao#saveCompany(Company company)
     */    
    public void saveCompany(final Company company) {
        getHibernateTemplate().saveOrUpdate(company);
    }

    /**
     * @see com.cmc.dao.CompanyDao#removeCompany(Long id)
     */
    public void removeCompany(final Long id) {
        getHibernateTemplate().delete(getCompany(id));
    }
}
